﻿/****************************************************************
* MACHINE: DESKTOP-UFHQLLI
* NAME: litian
* CREATEDATE: 2019/3/26 16:46:34
* DESC: <DESCRIPTION>
* **************************************************************/
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace LEE.Tools.Data
{
    /// <summary>
    /// Http request helper
    /// </summary>
    public class HttpClientHelper
    {
        private readonly static HttpClient httpClient;

        static HttpClientHelper()
        {
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
            var httpclientHandler = new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true
            };
            httpClient = new HttpClient(httpclientHandler);
            //设置接收数据类型
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }


        /// <summary>
        /// GET请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <returns></returns>
        public static T GetResponse<T>(string url) where T : class, new()
        {
            T result = default(T);
            HttpResponseMessage response = httpClient.GetAsync(url).Result;
            if (response.IsSuccessStatusCode)
            {
                var respStr = response.Content.ReadAsStringAsync().Result;
                result = JsonConvert.DeserializeObject<T>(respStr);
            }
            else
            {
                throw new Exception($"request error,the status code is {response.StatusCode}");
            }
            return result;
        }
        /// <summary>
        /// 发起post请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url">url</param>
        /// <param name="postData">post数据</param>
        /// <returns></returns>
        public static T PostAsJson<T>(string url, string postData) where T : class, new()
        {
            T result = default(T);
            HttpContent httpContent = new StringContent(postData);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                var respStr = response.Content.ReadAsStringAsync().Result;
                result = JsonConvert.DeserializeObject<T>(respStr);
            }
            else
            {
                throw new Exception($"request error,the status code is {response.StatusCode}");
            }
            return result;
        }

        /// <summary>
        /// 发起Put请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url">url</param>
        /// <param name="putData">put数据 json字符串</param>
        /// <returns></returns>
        public static T PutAsJson<T>(string url, string putData) where T : class, new()
        {
            T result = default(T);
            HttpContent httpContent = new StringContent(putData);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage response = httpClient.PutAsync(url, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                var respStr = response.Content.ReadAsStringAsync().Result;
                result = JsonConvert.DeserializeObject<T>(respStr);
            }
            else
            {
                throw new Exception($"request error,the status code is {response.StatusCode}");
            }
            return result;
        }

        /// <summary>
        /// 发起Delete请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url">url</param>
        /// <returns></returns>
        public static T Delete<T>(string url) where T : class, new()
        {
            T result = default(T);
            HttpResponseMessage response = httpClient.DeleteAsync(url).Result;
            if (response.IsSuccessStatusCode)
            {
                string respStr = response.Content.ReadAsStringAsync().Result;
                result = JsonConvert.DeserializeObject<T>(respStr);
            }
            else
            {
                throw new Exception($"request error,the status code is {response.StatusCode}");
            }
            return result;
        }
    }
}
